#include "ipmsg.h"
#include "server.h"
#include "commonFormats.h"
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
ip_Name ip_name;

/**
  *
  *
  */
extern int num_of_usr_online;
 extern struct HeartBeat heart_beat[20];
void Heart()
{

    printf("num_of_usr_online:%d\n",num_of_usr_online);

    int flag;
    int num;
    int timeNO;                             //flag用于判断修改是否成功   0-success   1-error
    char buffer[256] = {0};                     //buffer用于接收客户端发来的个人设置修改信息
    char IP[16] = {0};                          //IP用于存放发送者的IP

    clientRequest client_request;
    struct timeval time;

    while(1)
    {
        Recv_Request(buffer,IP,HEARTRBEAT);
        printf("%s\n",buffer);

        printf("%d\n",num_of_usr_online);
        printf("%s\n",heart_beat[num_of_usr_online - 1].name);

        client_request = Get_client_request(buffer);
        strcpy(client_request.sourceIP,IP);

        num = 0;
        flag = 0;
        while(num < num_of_usr_online&&flag != 1)
        {
            if(!strcmp(client_request.senderName,heart_beat[num].name))
            {
                gettimeofday(&time,NULL);

                heart_beat[num].last_time = heart_beat[num].now_time;

                heart_beat[num].now_time = time.tv_sec;

                timeNO = heart_beat[num].now_time - heart_beat[num].last_time;
                if(timeNO > 5)
                {
                        Change_state(client_request.senderName,'0');
//                        bzero(heart_beat[num].name,strlen(heart_beat[num]));
//                        heart_beat[num].last_time = 0;
                }
            }

            num++;
        }




    }


}

/////////////////////////////////////////////////////////////////////
//Setting用于专门接收个人设置修改发来的信息，并对相关文件做修改
void Setting()
{
    int flag;                                   //flag用于判断修改是否成功   0-success   1-error
    char buffer[256] = {0};                     //buffer用于接收客户端发来的个人设置修改信息
    char IP[16] = {0};                          //IP用于存放发送者的IP
    char buf[256] = {0};                        //buf用于存放返回给客户端的信息
    clientRequest client_request;               //client_request用于存放数据包的解析结果

    while(1)
    {
        Recv_Request(buffer,IP,SETTING_PORT);
        printf("%s\n",buffer);

        client_request = Get_client_request(buffer);
        strcpy(client_request.sourceIP,IP);

        Print(client_request);

        flag = Personal_setting(client_request);

        if(flag == 0)
        {
            strcat(buf,"success");
        }
        else
        {
            strcat(buf,"error");
        }
        printf("%s\n",buf);
        Reply_Request(buf,client_request.sourceIP,SETTING_PORT);

        bzero(buf,strlen(buf));

        printf("%s\n",client_request.sourceIP);
        printf("%s\n",buf);
    }


}

///////////////////////////////////////////////////////////////////////
//Get_IP_NAME用于解析从in.txt文件中读出的字符串

void Get_IP_NAME(char *buffer)
{
    int n;                                  //n用于表示 ~ 的个数
    unsigned int i,j;                       //i，j用于表示字符数组下标
    char ch;
    char buf[256] = {0};


    bzero(buf,sizeof(buf));
    buf[0] = '~';
    strcat(buffer,buf);
    bzero(buf,sizeof(buf));
    n = 0;
    for(i=0,j=0; i<strlen(buffer); i++,j++)
    {
        ch = buffer[i];
        if(ch == '~')
        {
            switch(n)                                               //n代表~的个数，根据n将字符串buf存入client_requestzhong
            {
            case 0:
            {
                bzero(ip_name.name,50);
                strcpy(ip_name.name,buf);
                printf("%s\n",buf);
                printf("ip_name.name:%s\n",ip_name.name);
                bzero(buf,sizeof(buf));
                //
                break;
            }
            case 1:
            {
                bzero(ip_name.IP,50);
                strcpy(ip_name.IP,buf);
                printf("%s\n",buf);
                printf("ip_name.IP:%s\n",ip_name.IP);
                bzero(buf,sizeof(buf));
                //
                break;

            }
            default:
                break;
            }
            n++;
            j=-1;
        }
        else
        {
            buf[j] = ch;
        }
    }

    printf("ip_name.name:%s\n",ip_name.name);
    printf("ip_name.IP:%s\n",ip_name.IP);
    //  return ip_name;

}

//////////////////////////////////////////////////////////////////////////
//当用户下线时，Delete_IP_NAME将用户名和对应的IP从in.txt文件中删除

void Delete_IP_NAME(clientRequest client_request)
{
    char ch;

    int num;                                           //num用于表示从文件中读出的字符串长度
    int flag;                                          //flag用于表示用户名和对应的IP是否删除成功，0-删除失败，1-删除成功
    char buf[BUFLEN] = {0};
    char buffer[BUFLEN] = {0};
    FILE *fd;
//    ip_Name *ip_name;
//    ip_name = (ip_Name *)malloc(sizeof(ip_Name));

    if((fd = fopen("in.txt","r+"))==NULL)
    {
        printf("open error");
        exit(1);
    }

    flag = 0;

    while(!feof(fd)&&flag != 1)
    {
        ch = fgetc(fd);
        buffer[0] = ch;
        if(ch == '\n')
        {
            buffer[0] = '~';
            strcat(buf,buffer);
            printf("%s\n",buf);
            Get_IP_NAME(buf);
            printf("ip_name.name:%s\n",ip_name.name);
            printf("client_request.senderName:%s\n",client_request.senderName);

            if(strcmp(ip_name.name, client_request.senderName) == 0)
            {
                fseek(fd,-(strlen(buf))+1,SEEK_CUR);                        //之前在buf末尾加了一个 ~ ，所以移文件指针时要加一位
                num = 0;
                while(num <strlen(buf)-1)
                {
                    fputc(' ',fd);

                    num++;
                }
                fputc('\n',fd);
                printf("%s\n",buf);
                flag = 1;


            }

            bzero(buf,strlen(buf));
        }
        else
        {
            strcat(buf,buffer);

        }
        bzero(buffer,strlen(buffer));
    }

    fclose(fd);

}

/////////////////////////////////////////////////////////////////////////
//当有用户上线时，Write_IP_NAME往in.txt文件中写入用户名和相应的IP

void Write_IP_NAME(clientRequest client_request)
{
    FILE *fd;


    if((fd = fopen("in.txt","a+"))==NULL)
    {
        printf("open error");
        exit(1);
    }

    fseek(fd,0,SEEK_END);
//    printf("flag:%d\n",flag);
//    fputs("111111111",fd);


    fputs(client_request.senderName,fd);
    printf("%s\n",client_request.senderName);
    fputc('~',fd);
    fputs(client_request.sourceIP,fd);
    fputc('\n',fd);

    fclose(fd);
}

/////////////////////////////////////////////////////////////////////////
//Personal_setting用于修改用户的个人信息

int Personal_setting(clientRequest setting)
{
    char ch;
    int num;
    int flag;                                 //用于判断用户信息修改是否成功，0-success，1-error
    char string[BUFLEN] = {0};                //将setting解析后存入string中，并string写入usr.txt中
    char buf[BUFLEN] = {0};
    char buffer[BUFLEN] = {0};
    FILE *fd;
    clientRequest client_request;

    if((fd = fopen("usr.txt","r+"))==NULL)
    {
        printf("open error");
        exit(1);
    }

    Print(setting);

    bzero(buf,strlen(buf));
    bzero(buffer,strlen(buffer));
    num = 0;
    flag = 1;

    while(!feof(fd)&&(flag != 0))
    {
        ch = fgetc(fd);
        buffer[0] = ch;
        if(ch == '\n')
        {
            client_request = Get_client_request(buf);
            Print(client_request);

            if(!strcmp(setting.senderName, client_request.senderName))
            {
                //  Print(client_request);
                //      printf("清空");
                fseek(fd,-(strlen(buf)),SEEK_CUR);

                num = 0;
                //     printf("清空");
                while(num<strlen(buf))
                {
                    fputc(' ',fd);
                    //  fprintf(fd,test);
                    num++;
                }

                fseek(fd,-(strlen(buf)),SEEK_CUR);
                init_Command(setting,string);
                printf("%s\n",string);

                fputs(string,fd);
                fputc('\n',fd);
                flag = 0;
            }

            bzero(buf,strlen(buf));
        }
        else
        {
            strcat(buf,buffer);

        }
        bzero(buffer,strlen(buffer));
    }

    fclose(fd);
    return flag;


}

////////////////////////////////////////////////////////////////////////////
//当有用户登陆时，Read_message从message.txt中读出该用户的离线信息，并将该信息发给该用户，并从message.txt中删除离线消息

void Read_message(char*NAME,char *IP)
{
    char ch;

    int num;
    char buf[BUFLEN] = {0};
    char buffer[BUFLEN] = {0};
    char string[BUFLEN] = {0};                           //string用于存放从文件中读出的信息，并将string发给用户
    clientRequest client_request;
    FILE *fd;

    if((fd = fopen("message.txt","r+"))==NULL)
    {
        printf("open error");
        exit(1);
    }

    bzero(buf,strlen(buf));
    bzero(buffer,strlen(buffer));

    printf("%s\n",NAME);


    while(!feof(fd))
    {
        ch = fgetc(fd);
        buffer[0] = ch;
        string[0] = 'M';

        if(ch == '\n')
        {
            client_request = Get_client_request(buf);
            Print(client_request);

            if(!strcmp(NAME , client_request.destName))
            {
                //  Print(client_request);

                strcat(string,buf);
                printf("%s\n",string);
                Reply_Request(string,client_request.destIP,PORT);
                printf("%s\n",IP);
                printf("%s\n",string);
                fseek(fd,-(strlen(buf)),SEEK_CUR);



                //     test = fgetc(fd);
                //    printf("%c\n",test);
                num = 0;

                while(num<strlen(buf))
                {
                    fputc(' ',fd);
                    //  fprintf(fd,test);
                    num++;
                }

                bzero(string,strlen(string));
            }

            bzero(buf,strlen(buf));
        }
        else
        {
            strcat(buf,buffer);

        }
        bzero(buffer,strlen(buffer));
    }
    fclose(fd);
}

/////////////////////////////////////////////////////////////////////////////
//Print用于打印clientRequest，只用于测试

void Print(clientRequest client_request)
{
    printf("client_request.requestType:%d\n",client_request.requestType);
    printf("client_request.senderName:%s\n",client_request.senderName);
    printf("client_request.destName:%s\n",client_request.destName);
    printf("client_request.senderPasswd:%s\n",client_request.senderPasswd);

    printf("client_request.sourceIP:%s\n",client_request.sourceIP);
    printf("client_request.destIP:%s\n",client_request.destIP);
    printf("client_request.nickName:%s\n",client_request.nickName);
    printf("client_request.state:%d\n",client_request.state);

    printf("client_request.gender:%s\n",client_request.gender);
    printf("client_request.age:%d\n",client_request.age);
    printf("client_request.branch:%s\n",client_request.branch);

    printf("client_request.telephone:%s\n",client_request.telephone);
    printf("client_request.birthdate:%s\n",client_request.birthdate);
    printf("client_request.profilepic:%d\n",client_request.profilepic);

    printf("client_request.msg:%s\n",client_request.msg);

}

/////////////////////////////////////////////////////////////////////////////
//init_Command用于将clientRequest组装成数据包，存入buffer中

void init_Command(clientRequest client_request,char *buffer)       //将结构体内容存入到buffer
{
    //    char buffer[256] = "";
    char buf[256]= {0};
    char str[256] = {0};
    bzero(buffer, strlen(buffer));

    str[0] = '~';
    //   memset(buffer,' ',sizeof(buffer));

    sprintf(buf,"%d",client_request.requestType);
    strcat(buffer,buf);
    strcat(buffer,str);

    strcat(buffer,client_request.senderName);
    strcat(buffer,str);

    strcat(buffer,client_request.destName);
    strcat(buffer,str);

    strcat(buffer,client_request.senderPasswd);
    strcat(buffer,str);

    strcat(buffer,client_request.sourceIP);
    strcat(buffer,str);

    strcat(buffer,client_request.destIP);
    strcat(buffer,str);

    strcat(buffer,client_request.nickName);
    strcat(buffer,str);

    bzero(buf,strlen(buf));
    sprintf(buf,"%d",client_request.state);
    strcat(buffer,buf);
    strcat(buffer,str);

    strcat(buffer,client_request.gender);
    strcat(buffer,str);

    bzero(buf,strlen(buf));
    sprintf(buf,"%d",client_request.age);
    strcat(buffer,buf);
    strcat(buffer,str);

    strcat(buffer,client_request.branch);
    strcat(buffer,str);

    strcat(buffer,client_request.telephone);
    strcat(buffer,str);

    strcat(buffer,client_request.birthdate);
    strcat(buffer,str);

    bzero(buf,strlen(buf));
    sprintf(buf,"%d",client_request.profilepic);
    strcat(buffer,buf);
    strcat(buffer,str);

    strcat(buffer,client_request.msg);
    strcat(buffer,str);




    /*
    unsigned int requestType;
    char senderName[NAMELEN];
    char senderPasswd[9];
    char sourceIP[16];
    char destIP[16];
    char nickName[NAMELEN];
    char state[1];
    char gender[1];
    unsigned int age;
    char branch[1];
    char telephone[12];
    char birthdate[12];
    unsigned int profilepic;
    char msg[MSGLEN];
    */





}

//////////////////////////////////////////////////////////////////////////////
//Write_message专门用于向文件中写入clientRequest体

void Write_message(FILE *fd,clientRequest client_request)
{
    int ret;
    char usr_age[256];
    char buf[256];
    char buffer[256];

    bzero(buf,sizeof(buf));
    bzero(buffer,sizeof(buffer));
    buf[0] = '~';
    buffer[0] = '\n';

    fseek(fd,-1,SEEK_CUR);

    ret = fputs(" ",fd);
    Print_error(ret);
    ret = fputs(buf,fd);
    Print_error(ret);

    ret = fputs(client_request.senderName,fd);
    Print_error(ret);
    ret = fputs(buf,fd);
    Print_error(ret);

    ret = fputs(client_request.destName,fd);
    Print_error(ret);
    ret = fputs(buf,fd);
    Print_error(ret);


    ret = fputs(" ",fd);
    Print_error(ret);
    ret = fputs(buf,fd);
    Print_error(ret);

    ret = fputs(client_request.sourceIP,fd);
    Print_error(ret);
    ret = fputs(buf,fd);
    Print_error(ret);

    ret = fputs(client_request.destIP,fd);
    Print_error(ret);
    ret = fputs(buf,fd);
    Print_error(ret);

    ret = fputs(client_request.nickName,fd);
    Print_error(ret);
    ret = fputs(buf,fd);
    Print_error(ret);

    sprintf(usr_age,"%d",client_request.state);
    ret = fputs(usr_age,fd);
    Print_error(ret);
    ret = fputs(buf,fd);
    Print_error(ret);

    ret = fputs(client_request.gender,fd);
    Print_error(ret);
    ret = fputs(buf,fd);
    Print_error(ret);

    sprintf(usr_age,"%d",client_request.age);
    ret = fputs(usr_age,fd);
    Print_error(ret);
    ret = fputs(buf,fd);
    Print_error(ret);

    ret = fputs(client_request.branch,fd);
    Print_error(ret);
    ret = fputs(buf,fd);
    Print_error(ret);

    ret = fputs(client_request.telephone,fd);
    Print_error(ret);
    ret = fputs(buf,fd);
    Print_error(ret);

    ret = fputs(client_request.birthdate,fd);
    Print_error(ret);
    ret = fputs(buf,fd);
    Print_error(ret);

    sprintf(usr_age,"%d",client_request.profilepic);
    ret = fputs(usr_age,fd);
    Print_error(ret);
    ret = fputs(buf,fd);
    Print_error(ret);

    ret = fputs(client_request.msg,fd);
    Print_error(ret);
    ret = fputs(buf,fd);
    Print_error(ret);

    ret = fputs(buffer,fd);
    //  Print_error(ret);



}

//////////////////////////////////////////////////////////////////////////////
//当用户不在线时，保存用户的离线消息

void Save_message(clientRequest client_request)
{

//   int ret;
    //  char usr_age[BUFLEN] = {0};


    FILE *fd;


    if((fd = fopen("message.txt","a+"))==NULL)
    {
        printf("open error");
        exit(1);
    }




    Write_message(fd,client_request);

    fclose(fd);



}

///////////////////////////////////////////////////////////////////////////////
//当用户上下线时，修改该用户的状态

void Change_state(char* name,char state)
{
    int flag;
    int ret;
    char ch ;
    char buf[BUFLEN] = {0};
    char buffer[BUFLEN] = {0};
    FILE *fd;

    if((fd = fopen("islogin.txt","r+"))==NULL)
    {
        printf("open error");
        exit(1);
    }

    bzero(buf,strlen(buf));
    bzero(buffer,strlen(buffer));
    flag = 0;
    while(!feof(fd) && (flag!= 1))                      //判断用户名是否已经存在
    {
        ch = fgetc(fd);
        buffer[0] = ch;
        if(ch != '~'&&ch != '\n')
        {
            strcat(buf,buffer);
        }
        else if(ch == '~')
        {
            if(0 == strcmp(buf,name))
            {
                flag = 1;
                bzero(buf,strlen(buf));
                ch = state;
                ret = fputc(ch,fd);
                ch =  '\n';
                ret = fputc(ch,fd);
                Print_error(ret);
            }
        }
        else
        {
            bzero(buf,strlen(buf));
        }
        bzero(buffer,strlen(buffer));
    }


    fclose(fd);
}

/////////////////////////////////////////////////////////////////////////////////
//当有用户请求上线时，判断该用户是否已经在线

int Is_Online(char* IP)
{
    int flag;
    int ret ;
    char ch ;
    char buf[BUFLEN] = {0};
    char buffer[BUFLEN] = {0};
    FILE *fd;

    if((fd = fopen("islogin.txt","r"))==NULL)
    {
        printf("open error");
        exit(1);
    }

    bzero(buf,strlen(buf));
    bzero(buffer,strlen(buffer));
    flag = 0;
    ret = 0;
    while(!feof(fd) && (flag!= 1))                      //判断用户名是否已经存在
    {
        ch = fgetc(fd);
        buffer[0] = ch;
        if(ch != '~'&&ch != '\n')
        {
            strcat(buf,buffer);
        }
        else if(ch == '~')
        {
            if(0 == strcmp(buf,IP))
            {
                flag = 1;
                bzero(buf,strlen(buf));
                buf[0] = fgetc(fd);
            }
        }
        else
        {
            bzero(buf,strlen(buf));
        }
        bzero(buffer,strlen(buffer));
    }
    fclose(fd);

    if(flag == 1)
    {
        ret = atoi(buf);
        return ret;                                         //0代表没上线，1代表上线
    }
    else
    {
        return 2;
    }



}

//////////////////////////////////////////////////////////////////////////////////
//当有用户请求注册时，判断该用户名是否已经被注册

int Is_Register(clientRequest client_request)
{
    int ret ;
    char ch ;
    char buf[BUFLEN] = {0};
    char buffer[BUFLEN] = {0};

    int flag;
    FILE *fd;

    if((fd = fopen("usrpwd.txt","r"))==NULL)
    {
        printf("open error");
        exit(1);
    }

    bzero(buf,strlen(buf));
    bzero(buffer,strlen(buffer));
    flag = 0;

    while(!feof(fd) && (flag!= 1))                      //判断用户名是否已经存在
    {
        ch = fgetc(fd);
        buffer[0] = ch;
        if(ch != '~'&&ch != '\n')
        {
            strcat(buf,buffer);
        }
        else if(ch == '~')
        {
            if(0 == strcmp(buf,client_request.senderName))
            {
                flag = 1;
                bzero(buf,strlen(buf));
            }
        }
        else
        {
            bzero(buf,strlen(buf));
        }
        bzero(buffer,strlen(buffer));
    }

    bzero(buf,strlen(buf));
    bzero(buffer,strlen(buffer));
    fclose(fd);

    if(flag == 1)                    //用户名已经存在，则返回1
    {
        return flag ;
    }
    else
    {
        buf[0] = '~';
        buffer[0] = '\n';
        fd = fopen("usrpwd.txt","a+");          //向usrpwd中写入用户名和密码
        if(fd == NULL)
        {
            printf("open error");
        }
        fseek(fd,-1,SEEK_CUR);
        ret = fputs(client_request.senderName,fd);
        Print_error(ret);
        ret = fputs(buf,fd);
        Print_error(ret);

        ret = fputs(client_request.senderPasswd,fd);
        Print_error(ret);
        ret = fputs(buffer,fd);
        Print_error(ret);
        fclose(fd);

        fd = fopen("islogin.txt","a+");          //向islogin.txt中写入用户名和登陆状态
        if(fd == NULL)
        {
            printf("open error");
        }
        fseek(fd,-1,SEEK_CUR);
        ret = fputs(client_request.senderName,fd);
        Print_error(ret);
        ret = fputs(buf,fd);
        Print_error(ret);

        ret = fputs("0",fd);
        Print_error(ret);
        ret = fputs(buffer,fd);
        Print_error(ret);
        fclose(fd);

        fd = fopen("usr.txt","a+");             //向usr中写入用户名、IP、昵称、主机名、性别、年龄、部门
        if(fd == NULL)
        {
            printf("open error");
        }
        fseek(fd,-1,SEEK_CUR);

        Write_message(fd,client_request);

        fclose(fd);

    }
    return flag;
}


//////////////////////////////////////////////////////////////////////////////////
//当有用户请求登陆时，判断该用户名和密码是否对应

int Is_Login(char* usr,char * pwd)
{
    int num;
    FILE *fd;
    char ch ;
    char buf[BUFLEN] = {0};
    char buffer[BUFLEN] = {0};
    if((fd = fopen("usrpwd.txt","r"))==NULL)
    {
        printf("open error");
        exit(1);
    }

    num =0;
    bzero(buf,strlen(buf));
    bzero(buffer,strlen(buffer));
    while( !feof(fd) && num!=2 )
    {
        ch = fgetc(fd);
        buffer[0] = ch;
        if(ch != '~'&&ch != '\n')
        {
            strcat(buf,buffer);


        }
        else if(ch == '~')
        {
            if(0 == strcmp(buf,usr))                    //判断时必须要用户名相同才判断密码是否正确
            {
                num++;
                bzero(buf,strlen(buf));
            }
        }
        else
        {

            if( num==1 && 0 == strcmp(pwd,buf) )
            {
                num++;

            }
            else
            {
                num=0;
            }
            bzero(buf,strlen(buf));
        }
        bzero(buffer,strlen(buffer));
    }

    fclose(fd);

    if(num == 2)
        return 0;                       //返回0，代表登陆成功，返回，1代表登陆失败
    else
        return 1;
}

//////////////////////////////////////////////////////////////////////////////////
//将接收到的包解析，并将结果存入到clientRequest

clientRequest Get_client_request(char* buffer)
{
    clientRequest client_request;
    int num;
    int n;
    unsigned int i,j;
    char ch;
    char buf[BUFLEN] = {0};

    bzero(buf,sizeof(buf));
    n = 0;
    for(i=0,j=0; i<strlen(buffer); i++,j++)
    {
        ch = buffer[i];
        if(ch == '~')
        {
            switch(n)                                               //n代表~的个数，根据n将字符串buf存入client_requestzhong
            {
            case 0:
            {
                num = atoi(buf);
                client_request.requestType = num;
                bzero(buf,sizeof(buf));
                //
                break;
            }
            case 1:
            {
                bzero(client_request.senderName,NAMELEN);
                strcpy(client_request.senderName,buf);
                bzero(buf,sizeof(buf));
                //
                break;

            }
            case 2:
            {
                bzero(client_request.destName,NAMELEN);
                strcpy(client_request.destName,buf);
                bzero(buf,sizeof(buf));
                //
                break;
            }
            case 3:
            {
                bzero(client_request.senderPasswd,NAMELEN);
                strcpy(client_request.senderPasswd,buf);
                bzero(buf,sizeof(buf));
                //
                break;
            }
            case 4:
            {
                bzero(client_request.sourceIP,NAMELEN);
                strcpy(client_request.sourceIP,buf);
                bzero(buf,sizeof(buf));
                break;
            }
            case 5:
            {
                bzero(client_request.destIP,NAMELEN);
                strcpy(client_request.destIP,buf);
                bzero(buf,sizeof(buf));
                //
                break;
            }
            case 6:
            {
                bzero(client_request.nickName,NAMELEN);
                strcpy(client_request.nickName,buf);
                bzero(buf,sizeof(buf));
                //
                break;
            }
            case 7:
            {
                num = atoi(buf);
                client_request.state= num;
                bzero(buf,sizeof(buf));
                //
                break;
            }
            case 8:
            {
                bzero(client_request.gender,NAMELEN);
                strcpy(client_request.gender,buf);
                bzero(buf,sizeof(buf));

                //
                break;
            }
            case 9:
            {
                num = atoi(buf);
                client_request.age = num;
                bzero(buf,sizeof(buf));
                //                 printf("%d\n",num);
                break;
            }
            case 10:
            {
                bzero(client_request.branch,NAMELEN);
                strcpy(client_request.branch,buf);
                bzero(buf,sizeof(buf));
                //
                break;
            }
            case 11:
            {
                bzero(client_request.telephone,NAMELEN);
                strcpy(client_request.telephone,buf);
                bzero(buf,sizeof(buf));
                //
                break;
            }
            case 12:
            {
                bzero(client_request.birthdate,NAMELEN);
                strcpy(client_request.birthdate,buf);
                bzero(buf,sizeof(buf));
                //
                break;
            }
            case 13:
            {
                num = atoi(buf);
                client_request.profilepic = num;
                bzero(buf,sizeof(buf));
                break;
            }
            case 14:
            {
                bzero(client_request.msg,NAMELEN);
                strcpy(client_request.msg,buf);
                bzero(buf,sizeof(buf));
                break;
            }
            }

            num = 0;
            n++;
            j=-1;
        }
        else
        {
            buf[j] = ch;
        }

    }
//   Print( client_request);
    return client_request;
}

//////////////////////////////////////////////////////////////////////////////////
//用于接收用户发送的数据包，其中buffer用于接收数据包，IP用于存放发送者的ip，port为接收端口

void  Recv_Request(char *buffer,char *IP,int port)
{
    int ret;
    int socket_fd;
    struct sockaddr_in addr, recvfrom_addr;
    int sin_len;

    socket_fd = socket(AF_INET, SOCK_DGRAM, 0);
    if(socket_fd == -1)
    {
        perror("socket");
        exit(1);
    }

    addr.sin_family = AF_INET;
    addr.sin_addr.s_addr = htonl(INADDR_ANY);
    addr.sin_port = htons(port);

    sin_len = sizeof(recvfrom_addr);

    ret = bind(socket_fd,(struct sockaddr*)&addr,sizeof(addr));
    if(ret == -1)
        ERR(bind)

        setsockopt(socket_fd, SOL_SOCKET, SO_BROADCAST,&socket_fd,sizeof(socket_fd));

    if(recvfrom(socket_fd
                , buffer
                , BUFLEN
                , 0
                , (struct sockaddr*)&recvfrom_addr
                , &sin_len)==-1)
        ERR(recv)
//   IP = inet_ntoa(recvfrom_addr.sin_addr);
        //  sprintf(IP,"%s",inet_ntoa(recvfrom_addr.sin_addr));
        //  strcpy(IP,inet_ntoa(recvfrom_addr.sin_addr));
        strcpy(IP,inet_ntoa(recvfrom_addr.sin_addr));                       //将发送者赋值给变量IP
    close(socket_fd);
}


//////////////////////////////////////////////////////////////////////////////////
//用于发送的数据包，其中buffer用于发送的数据包，IP用于存放目的ip，port为接收端口


void Reply_Request(char * buffer ,char*  IP,int port)
{
    int ret;
    int socket_fd;
    struct sockaddr_in add;
    //char buffer[BUFLEN] = " ";             //buffer用于存储所发消息的内容

    //初始化sockaddr_in结构体
    add.sin_family = AF_INET;
    add.sin_port = htons(port);
    add.sin_addr.s_addr =inet_addr(IP);

    socket_fd = socket(AF_INET,SOCK_DGRAM,0);
    if(socket_fd == -1)
        ERR(socket)

        ret = sendto(socket_fd,buffer,BUFLEN,0,(struct sockaddr*)&add,sizeof(add));
    if(ret == -1)
        ERR(sendto)
    }






void Print_error(int ret)
{
    if(ret == -1)
    {
        printf("error");
    }
}



////////////////////////////////////////////////////////////////////////////////
//当用户登陆成功后，向用户发送各种数据，包括其他用户信息、用户ip、用户状态


void Send_usrinfo(char *IP)
{
    char ch;
    char string[BUFLEN] = {0};
    char buf[BUFLEN] = {0};
    char buffer[BUFLEN] = {0};
    FILE *fd;

    if((fd = fopen("usr.txt","r"))==NULL)
    {
        printf("open error");
        exit(1);
    }

    bzero(buf,strlen(buf));
    bzero(buffer,strlen(buffer));
    while(!feof(fd))
    {
        ch = fgetc(fd);
        buf[0] = ch;

        if(ch == '\n')
        {
            printf("%s\n",buffer);
            Reply_Request(buffer, IP,PORT);
            usleep(10000);

            //           printf("send success");

            printf("%d\n",strlen(buffer));
            bzero(buffer,strlen(buffer));
        }
        else
        {
            strcat(buffer,buf);
        }
    }
    fclose(fd);

    bzero(buf,strlen(buf));
    bzero(buffer,strlen(buffer));

//    strcat(buf,"0");
//    Reply_Request(buf, IP);
//    printf("%s\n",buf);

    bzero(buf,strlen(buf));
    bzero(buffer,strlen(buffer));
    bzero(string,sizeof(string));

    if((fd = fopen("islogin.txt","r"))==NULL)
    {
        printf("open error");
        exit(1);
    }

    bzero(buf,strlen(buf));
    bzero(buffer,strlen(buffer));

    while(!feof(fd))
    {
        ch = fgetc(fd);
        buf[0] = ch;
        string[0] = 's';
        string[1] = '~';

        if(ch == '\n')
        {
            printf("%s\n",buffer);
            strcat(string,buffer);
            Reply_Request(string, IP,PORT);
            usleep(10000);
            //           printf("send success");


            bzero(buffer,strlen(buffer));
            bzero(string,strlen(string));
        }
        else
        {
            strcat(buffer,buf);
        }
    }


    bzero(buf,strlen(buf));
    bzero(buffer,strlen(buffer));
    bzero(string,sizeof(string));

    if((fd = fopen("in.txt","r"))==NULL)
    {
        printf("open error");
        exit(1);
    }

    bzero(buf,strlen(buf));
    bzero(buffer,strlen(buffer));

    while(!feof(fd))
    {
        ch = fgetc(fd);
        buf[0] = ch;
        string[0] = 'I';
        string[1] = '~';

        if(ch == '\n')
        {

            if(strlen(buffer))
            {
                printf("%s\n",buffer);
                strcat(string,buffer);
                Reply_Request(string, IP,PORT);
                usleep(10000);
            }

            //           printf("send success");


            bzero(buffer,strlen(buffer));
            bzero(string,strlen(string));
        }
        else
        {
            strcat(buffer,buf);
        }
    }

    fclose(fd);
    bzero(buffer,strlen(buffer));
    strcat(buffer,"woca");
    Reply_Request(buffer, IP,PORT);
    printf("%s\n",buffer);
    bzero(buffer,strlen(buffer));

}

